package tt.dz.order.pojo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.util.AmountUtil;
import tt.dz.order.util.OrderConstant.OrderServiceEnum;
import tt.dz.order.util.OrderConstant.OrderStatus;
import tt.dz.order.vo.OrderBaseData;

/**
 * @Title: 订单项信息
 * @author zhoutian   
 * @date 2015-11-26
 */
public class OrderItem {
	//订单项id 2016/11/17
	private String itemId;
	//所属订单编号
	private String orderNo;
	//所属用户id，表示是谁的订单
	private String userId;
	//服务编号
	private String serviceNo;
	//提供服务的运营商编号
	private String serviceProvider;
	//该运营商提供的服务是否是成本类的服务，0：否 1：是
	private int isCost;
	//计量单位编号
	private int unitNo;
	//原始单价，来源于服务表的unitPrice
	private BigDecimal unitPrice;
	//实际单价,活动打折后的实际单价
	private BigDecimal realUnitPrice;
	//购买数量
	private BigDecimal buyNum;
	//赠送数量
	private BigDecimal freeNum;
	//原始总价
	private BigDecimal totalPrice;
	//实际总价
	private BigDecimal realTotalPrice;
	//匹配的活动规则
	private String activityRule;
	//服务站编号，代表设备所在地
	private String serviceStationNo;
	//设备编号,表示该服务是哪个设备提供的，比如桩的编号
	private String equipmentNo;
	//辅助设备编号,当设备编号不能唯一说明时，需要与本字段结合进行标识
	private String auxEquipmentNo;
	//服务开始时间
	private Date startTime;
	//服务结束时间
	private Date endTime;
	//创建时间
	private Date createTime;
	//订单项描述信息
	private String message;
	private transient OrderBaseData orderBaseData;

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getServiceProvider() {
		return serviceProvider;
	}

	public void setServiceProvider(String serviceProvider) {
		this.serviceProvider = serviceProvider;
	}

	public int getIsCost() {
		return isCost;
	}

	public void setIsCost(int isCost) {
		this.isCost = isCost;
	}

	public int getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(int unitNo) {
		this.unitNo = unitNo;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public BigDecimal getRealUnitPrice() {
		return realUnitPrice;
	}

	public void setRealUnitPrice(BigDecimal realUnitPrice) {
		this.realUnitPrice = realUnitPrice;
	}

	public BigDecimal getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(BigDecimal buyNum) {
		this.buyNum = buyNum;
	}

	public BigDecimal getFreeNum() {
		return freeNum;
	}

	public void setFreeNum(BigDecimal freeNum) {
		this.freeNum = freeNum;
	}

	public BigDecimal getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(BigDecimal totalPrice) {
		this.totalPrice = totalPrice;
	}

	public BigDecimal getRealTotalPrice() {
		return realTotalPrice;
	}

	public void setRealTotalPrice(BigDecimal realTotalPrice) {
		this.realTotalPrice = realTotalPrice;
	}

	public String getActivityRule() {
		return activityRule;
	}

	public void setActivityRule(String activityRule) {
		this.activityRule = activityRule;
	}

	public String getServiceStationNo() {
		return serviceStationNo;
	}

	public void setServiceStationNo(String serviceStationNo) {
		this.serviceStationNo = serviceStationNo;
	}

	public String getEquipmentNo() {
		return equipmentNo;
	}

	public void setEquipmentNo(String equipmentNo) {
		this.equipmentNo = equipmentNo;
	}

	public String getAuxEquipmentNo() {
		return auxEquipmentNo;
	}

	public void setAuxEquipmentNo(String auxEquipmentNo) {
		this.auxEquipmentNo = auxEquipmentNo;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public OrderBaseData getOrderBaseData() {
		return orderBaseData;
	}

	public void setOrderBaseData(OrderBaseData orderBaseData) {
		this.orderBaseData = orderBaseData;
	}

	public OrderItem() {
		
	}
	
	/**
	 * 用订单基础数据构造订单项里的部分数据
	 * @param orderBaseData 订单基础数据
	 */
	public OrderItem(OrderBaseData orderBaseData) {
		this.userId = orderBaseData.getUserId();
		this.itemId = orderBaseData.getItemId();
		this.serviceNo = orderBaseData.getServiceNo();
		this.serviceProvider = orderBaseData.getServiceProviderCode();
		this.unitNo = orderBaseData.getUnitNo();
		this.unitPrice = orderBaseData.getUnitPrice();
		this.realUnitPrice = orderBaseData.getUnitPrice();
		this.buyNum = orderBaseData.getBuyNum();
		BigDecimal price = orderBaseData.getTotalPrice();
		if(price == null){
			price = AmountUtil.multiply(orderBaseData.getUnitPrice(), orderBaseData.getBuyNum());
		}
		this.totalPrice = price;
		this.realTotalPrice = price;
		this.serviceStationNo = orderBaseData.getPlantId();
		this.equipmentNo = orderBaseData.getEquipmentNo();
		this.auxEquipmentNo = orderBaseData.getAuxEquipmentNo();
		this.startTime = orderBaseData.getStartTime();
		this.endTime = orderBaseData.getEndTime();
		this.message = orderBaseData.getMessage();
		this.createTime = new Date();
	}
	
	public void save(){
		String sql = "insert into order_item(itemid,orderNo, userId, serviceNo, serviceProvider, isCost, unitNo, unitPrice, realUnitPrice, buyNum, freeNum, totalPrice, realTotalPrice, activityRule, serviceStationNo, equipmentNo, auxEquipmentNo, startTime, endTime, createTime,message) values(:itemId, :orderNo, :userId, :serviceNo, :serviceProvider, :isCost, :unitNo, :unitPrice, :realUnitPrice, :buyNum, :freeNum, :totalPrice, :realTotalPrice, :activityRule, :serviceStationNo, :equipmentNo, :auxEquipmentNo, :startTime, :endTime, :createTime,:message)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	/**
	 * 根据订单号获取订单项
	 * @return 订单列表
	 */
	public static List<OrderItem> getByOrderNo(String orderNo){
		String sql = "select itemid,orderNo, userId, serviceNo, serviceProvider, isCost, unitNo, unitPrice, realUnitPrice, buyNum, freeNum, totalPrice, realTotalPrice, activityRule, serviceStationNo, equipmentNo, auxEquipmentNo, startTime, endTime, createTime,message from order_item where orderNo = :orderNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNo", orderNo);
		return BaseDAOUtil.queryForList(sql, paramMap, OrderItem.class);
	}
	
	/**
	 * 获取充电预约订单项
	 * @return 单个订单项
	 */
	public static OrderItem getChargeAppointmentOrderItemBy(String userId, String orderNo){
		String sql = "select itemid,orderNo, userId, serviceNo, serviceProvider, isCost, unitNo, unitPrice, realUnitPrice, buyNum, freeNum, totalPrice, realTotalPrice, activityRule, serviceStationNo, equipmentNo, auxEquipmentNo, startTime, endTime, createTime,message from order_item where userId = :userId and orderNo = :orderNo and serviceNo = :serviceNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		paramMap.put("orderNo", orderNo);
		paramMap.put("serviceNo", OrderServiceEnum.ChargeAppointment.value());
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderItem.class);
	}
	
	/**
	 * 获取用户在某个电桩/枪上，离当前时间最近的充电预约订单
	 * @return 单个订单项
	 */
	public static OrderItem getChargeAppointmentOrderItemBy(String userId, String equipmentNo, String auxEquipmentNo, Date beforeNow, Date now){
		String sql = "select order_item.itemid,order_item.orderNo, order_item.userId, serviceNo, serviceProvider, isCost, unitNo, unitPrice, realUnitPrice, buyNum, freeNum, totalPrice, realTotalPrice, order_item.activityRule, serviceStationNo, equipmentNo, auxEquipmentNo, startTime, endTime, order_item.createTime,order_item.message"
				+ " from order_item inner join order_info on order_item.orderNo = order_info.orderNo where order_item.serviceNo = :serviceNo and userId = :userId and equipmentNo = :equipmentNo and auxEquipmentNo = :auxEquipmentNo and order_info.status = " + OrderStatus.payedDone.value() + " and startTime >= :beforNow and startTime < :now order by startTime desc";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		paramMap.put("equipmentNo", equipmentNo);
		paramMap.put("auxEquipmentNo", auxEquipmentNo);
		paramMap.put("serviceNo", OrderServiceEnum.ChargeAppointment.value());
		paramMap.put("beforNow", beforeNow);
		paramMap.put("now", now);
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderItem.class);
	}
	/**
	 * 更新订单项金额
	 */
	public void updateOrderItem(){
		String sql = "update order_item set unitPrice=:unitPrice,buyNum=:buyNum,realTotalPrice=:realTotalPrice ,totalPrice=:totalPrice where itemid=:itemId";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("realTotalPrice", realTotalPrice);
		paramMap.put("buyNum", buyNum);
		paramMap.put("unitPrice", unitPrice);
		paramMap.put("itemId", itemId);
		paramMap.put("totalPrice", totalPrice);
		BaseDAOUtil.update(sql, paramMap);
	}
}
